﻿Imports VendasBLL
Imports VendasDAL
Public Class FuncionarioFRM
    Dim ObjBLL As New FuncionarioBLL
    Public Op As String
    Public codigo As Integer
    Public CodCargo As Integer
#Region "Carrega Campos"
    Sub CarregaCampos()
        ObjBLL = New FuncionarioBLL(codigo)
        LBCodigo.Text = "Código: " & ObjBLL.Codigo
        LBTitulo.Text = ObjBLL.Nome
        TXTComplemento.Text = ObjBLL.Complemento
        TXTNome.Text = ObjBLL.Nome
        MTBCep.Text = ObjBLL.Cep
        MTBCPF.Text = ObjBLL.cpf
        TXTRG.Text = ObjBLL.rg
        CodCargo = ObjBLL.CodCargo
        TXTCargo.Text = ObjBLL.Cardescricao
        TXTBairro.Text = ObjBLL.Bairro
        TXTCidade.Text = ObjBLL.Cidade
        TXTEndereco.Text = ObjBLL.Endereco
        TXTNum.Text = ObjBLL.Num
        CBUf.SelectedValue = ObjBLL.Uf
        MTBResidencial.Text = ObjBLL.telres
        MTBCelular.Text = ObjBLL.telcel
        MTBFax.Text = ObjBLL.telfax
        TXTObs.Text = ObjBLL.Obs
        TXTEmail.Text = ObjBLL.Email
        CBStatus.SelectedValue = ObjBLL.Status
        DTPNascimento.Text = ObjBLL.DtNascimento
        TXTSalario.Text = ObjBLL.Salario
        TXTCarteira.Text = ObjBLL.Num
        DTPAdmissao.Text = ObjBLL.DtAdmissao
        DTPDemissao.Text = ObjBLL.DtDemissao
    End Sub
#End Region

#Region "Limpar campos"
    Sub Limpa()
        CBStatus.SelectedValue = 1
        TXTNome.Text = ""
        MTBCPF.Text = ""
        TXTRG.Text = ""
        TXTSalario.Text = "0,00"
        TXTCarteira.Text = ""
        TXTBairro.Text = ""
        TXTCidade.Text = ""
        TXTEndereco.Text = ""
        TXTNum.Text = ""
        CBUf.SelectedValue = ""
        MTBResidencial.Text = ""
        MTBCelular.Text = ""
        MTBFax.Text = ""
        TXTObs.Text = ""
        TXTEmail.Text = ""
        MTBCep.Text = ""
        TXTComplemento.Text = ""
        TXTCargo.Text = ""
        CodCargo = 0
        DTPNascimento.Text = Date.Now
        Me.ObjBLL = Nothing
    End Sub
#End Region

#Region "Load"

    Private Sub ClienteFRM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        objconexao.carregaCombo(CBStatus, "Generico.Gen_Status", "sta_codigo", "sta_descricao", "sta_codigo < 3", "sta_codigo")
        objconexao.carregaCombo(CBUf, "Generico.Gen_Estado", "est_uf", "est_descricao", "", "est_uf")
        Limpa()
        If (Op = "I") Then
            LBCodigo.Visible = False
            LBTitulo.Text = "Funcionário"
            LBCodigo.Text = 0
            LBDemissao.Visible = False
            DTPDemissao.Visible = False
            DTPAdmissao.Enabled = True

        Else
            LBCodigo.Visible = True
            DTPAdmissao.Enabled = False
            CarregaCampos()
        End If
    End Sub
#End Region

#Region "Salvar/Alterar"
    Private Sub TSBSalvar_Click(sender As System.Object, e As System.EventArgs) Handles TSBSalvar.Click

        Try

            ObjBLL = New FuncionarioBLL(codigo, StrConv(Trim(TXTNome.Text), VbStrConv.ProperCase), StrConv(Trim(TXTEndereco.Text), VbStrConv.ProperCase), TXTNum.Text,
              StrConv(Trim(TXTBairro.Text), VbStrConv.ProperCase), StrConv(Trim(TXTCidade.Text), VbStrConv.ProperCase), CBUf.SelectedValue,
              StrConv(Trim(TXTComplemento.Text), VbStrConv.ProperCase), MTBCep.Text.Replace(" ", "").Replace("-", ""), Trim(TXTEmail.Text), Trim(TXTObs.Text),
              DTPNascimento.Text, CBStatus.SelectedValue, Trim(MTBCPF.Text).Replace("-", "").Replace(" ", "").Replace(",", ""), Trim(TXTRG.Text), Trim(MTBResidencial.Text).Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", ""),
              Trim(MTBCelular.Text).Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", ""),
             Trim(MTBFax.Text).Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", ""), Trim(TXTCarteira.Text), DTPAdmissao.Text, DTPDemissao.Text, CodCargo, Trim(TXTSalario.Text).Replace("R", "").Replace("$", "").Replace(" ", ""))

            If Op = "I" Then
                ObjBLL.InserirAlterar()
                MessageBox.Show("Funcionário inserido com sucesso", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Limpa()
                Me.Close()
            Else
                ObjBLL.InserirAlterar()
                MessageBox.Show("Funcionário alterada com sucesso", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Limpa()
                Me.Close()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub
#End Region

#Region "Voltar"
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        Me.Close()
    End Sub
#End Region

#Region "Localizar cargo"
    Private Sub BCargo_Click(sender As System.Object, e As System.EventArgs) Handles BCargo.Click
        ConsultaCargosFRM.Tipo = "ret"
        ConsultaCargosFRM.ShowDialog()
    End Sub
#End Region

#Region "Validação somente número no campo carteira"
    Private Sub TXTCarteira_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTCarteira.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
    End Sub
#End Region

    Private Sub TXTRG_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTRG.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
    End Sub

    Private Sub TXTSalario_Validated(sender As System.Object, e As System.EventArgs) Handles TXTSalario.Validated
        TXTSalario.Text = FormatCurrency(TXTSalario.Text)
    End Sub

    Private Sub CBStatus_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBStatus.SelectedIndexChanged
        If (CBStatus.SelectedIndex = 0) Then
            LBDemissao.Visible = False
            DTPDemissao.Visible = False
        Else
            LBDemissao.Visible = True
            DTPDemissao.Visible = True
        End If
    End Sub

    Private Sub TXTSalario_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTSalario.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "," Then
            e.Handled = True
        End If
    End Sub

    Private Sub FuncionarioFRM_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If (e.KeyCode = Keys.Escape) Then
            Me.Close()
        End If
    End Sub
End Class